Methods and systems for applications to interact with hardware

ABSTRACT

A method of providing Java application layer access to hardware peripheral memory mapped registers is provided together with a processor adapted to implement such a method. A fixed memory address space for a hardware peripheral&#39;s memory mapped registers is identified, and a Java object is constructed having elements which occupy this fixed memory address space. This allows a Java application to be provided with access to the hardware peripheral&#39;s memory mapped registers directly through the Java object. A new Java class is defined having base address and length parameters and in some cases also having a type parameter. This is used in constructing the Java object. When a Java object has an object descriptor which is effectively an object header and a pointer to where the object data is located, constructing the Java object may be done by creating an object descriptor, and then creating an object handle for the Java object which points to the object descriptor. Alternatively, a level of indirection may be removed, and the object handle created to point directly to the object created so as to exist in memory mapped register space.

FIELD OF THE INVENTION

The invention relates to methods and systems for applications tointeract with hardware, and more particularly for applications inlanguages such as Java which lack an ability to access particularmemories directly, to interact with hardware peripherals.

BACKGROUND OF THE INVENTION

Typical software systems include an application program run by anoperating system on a processor connected to a number of hardwareperipherals. In some systems, such as those where the Java language isemployed, there is additionally a virtual machine such as a Java virtualmachine (JVM) situated between the operating system and the applicationprogram.

In order to facilitate communication with the hardware peripherals,nearly all complex software systems include a device driver which isspecific to each hardware peripheral. These device drivers provide alayer of abstraction to their clients (the operating system andultimately the application program) while allowing them to use theunderlying hardware peripherals.

However, device drivers are notoriously difficult to debug ortroubleshoot due to the asynchronous nature of their coupling withinterrupts and due the lack of debugging features. A faulty driver canalso inhibit user input and/or user output. At interrupt levels, thesystem cannot provide support for the common user state input/outputfunctionality. Furthermore, since timing is often critical, it isimpossible to stop a processor and trace device driver code in anon-destructive way. Also, because of the way systems are developed,frequently different development teams are responsible for differentlayers of a design. When interface problems develop, it is oftendifficult to determine where the problem originated and hence which teamshould fix the problem.

Consequently, device drivers take more time to develop and their opacitymakes them more error-prone. One of the most common errors which mayoccur during driver development is an error in pointer arithmetic whichinstructs the processor to access an erroneous location. Systemsgenerally allow this initially and operation resumes without anyapparent disturbance until a later point in time where the value inquestion is used.

While this is not a big problem for large computing platforms withstandardized peripheral interfaces, and a standardized layeredarchitecture, it becomes a very serious problem for application specifichardware and devices where for each new design, the applicationperipheral path must be debugged from scratch.

Common safeguard measures against pointer arithmetic errors includesoftware range checking. Some languages, such as Java, have inherentmeasures which prevent invalid memory accesses. However, using thebuilt-in range checking of standard Java to develop device drivers iscurrently impossible as one of the fundamental characteristics of Javais that any client machine should be protected from corruption/bugs inJava, i.e. any bug in a Java application should only effect the Javaapplication and should have no effect on other applications and memoryunrelated to the Java application. To achieve this level of security,Java applications running on the JVM are not given direct access tomemory. Instead, memory access is done through an indirection mechanismthrough the JVM.

Referring now to FIG. 1, a conventional embedded environment 3 typicallyhas hardware 2, software in the form of native code 4 (or assemblylanguage), and software in the form of a Java application 6. Also shownare externally connected hardware peripherals 8. The hardware 2 consistsof a processor core 10, memory in the form of RAM 12 and/or ROM 14 andone or more physical interfaces 18 including for example a serial port27. The software 4 running on the processor 10 includes an operatingsystem 20 over top of which is run a Java virtual machine 22 as a task,and also over which other tasks such as an event dispatcher task 23 isrun. The Java application 6 uses the resources and features of the Javavirtual machine 22.

FIG. 1 also shows the details of a typical path from the Javaapplication 6 to and from a particular hardware peripheral 8 which forthe purpose of this example we will assume is the serial port peripheral26 connected through the serial port 27. The Java application 6 includesfunctionality 28 for either generating data ultimately for output to theserial port 27, or for processing data ultimately received from theserial port 27. Of course the functionality 28 does not interact withthe serial port 27 directly. The Java virtual machine 22 has a Javanative interface 30 through which the Java application 6 communicateswith the serial port physical interface 27. The operating system 20 hasa serial port device driver 32 which has an input queue 34 and an outputqueue 36, through which it communicates with the underlying hardware 2.The serial port device driver 32 is typically run at interrupt level, orthrough a deferred procedure call within the operating system kernel(not shown). More specifically, the serial port device driver 32communicates with the serial port 27 through serial port memory mappedregisters 56 to an input queue 38 and an output queue 40 and on to thehardware peripheral 26. The operating system 20 also has an IRQ(interrupt request) handler 33 for each interrupt from any hardwareperipheral.

When the Java application 6 has to communicate with the hardwareperipherals 8 and in this case the serial port peripheral 26, a pathsuch as that consisting of the serial port communications 28→Javavirtual machine 22→Java native interface 30→operating system 20→serialport device driver 32→serial port physical interface 27→serial porthardware peripheral 26 must be established and debugged for eachdifferent hardware peripheral. More specifically, when the Javaapplication 6 has data to send to the serial port peripheral 28, theJava application 6 communicates with the device driver 32 using the Javanative interfaces 30. The JNI 30 takes the data, formats it and passesit on to the device driver 32 by copying it into the output buffer 34.The serial port device driver 32 transfers the data to the serial portmemory mapped registers 56 of the serial port 27. These are copied intothe hardware queue 38 in the serial port 27 for output.

When the serial port 27 receives data destined for the Java application6, an even more complicated path is taken. For communication originatingfrom the hardware, the process typically goes as follows. To begin, thearrival of data at the serial port 27 triggers the assertion of ahardware interrupt. When this occurs, the program flow is interrupted,and the IRQ handler 33 starts an interrupt service routine. Theinterrupt service routine calls the serial port device driver 32 whichreads the data from the hardware input queue 40 in the serial port 27and copies it into the input queue 36 which is one of the devicedriver's data structures. The serial port device driver 32 then posts anevent to the event dispatcher task 23. The interrupt service routinereturns and normal Java operation resumes. The event dispatcher task 23sends an event to one of the destination threads 6 to read from theinput queue 36 of the serial port device driver 32, for example througha piping mechanism provided by the JNI 30.

It can be clearly seen that there are a large number of areas where bugsmay make their way into the design of such a Java application—hardwareperipheral interaction. Furthermore, each copying stage forces powerconsuming and processor intensive operations which are inevitable due tothe abstractions of the operating system 20 and the JVM 22.

SUMMARY OF THE INVENTION

It is an object of the invention to obviate or mitigate one or more ofthe above-identified disadvantages.

One embodiment of the invention provides a method of providing Javaapplication layer access to hardware peripheral memory mapped registers.A fixed memory address space for a hardware peripheral's memory mappedregisters is identified, and a Java object is constructed havingelements which occupy this fixed memory address space. This allows aJava application to be provided with access to the hardware peripheral'smemory mapped registers directly through the Java object.

A new Java class may be defined having base address and lengthparameters and in some cases also having an element length parameter.This is used in constructing the Java object.

In an embodiment applicable when a Java object has an object descriptorwhich is effectively an object header and a pointer to where the objectdata is located, constructing the Java object may be done by creating anobject descriptor, and then creating an object handle for the Javaobject which points to the object descriptor.

Preferably, the new Java class having a class name <class name>, forexample, “AnchoredArray” is defined as follows:

<class name> (base, length)

where <class name> is the name assigned to the new class. Optionally, aparameter “type” might also be provided which specifies the type ofobject to be created. If multiple types are not contemplated, then thisparameter would not be required. In the examples which follow, a defaulttype of integer array is assumed. The parameter “base” specifies abeginning address, and length is a parameter specifying a number ofelements in the object, which when constructed, generates an objectdescriptor having the specified type, base, length, and also generates ahandle to the object descriptor.

In another embodiment, a level of indirection is removed, and the objecthandle points directly to the object created so as to exist in memorymapped register space. In so doing, preferably, a memory map is definedhaving a predetermined address space for the peripheral, and having atleast one additional address space allocated contiguous with thepredetermined address space. Object header information for the Javaobject is stored directly in the additional address space.

Other embodiments provide a Java object defined such that it overlapswith a predetermined address space; a Java class which enables a Javaobject to be defined such that it overlaps with a predetermined addressspace; and a Java virtual machine featuring such a class. Yet anotherembodiment provides an integrated circuit having a plurality ofperipheral memory mapped registers and a Java virtual machine which hasJava objects anchored to said peripheral memory mapped registers.

Advantageously, the embodiments of the invention permit portions ofmemory space defined by the object descriptor or header to be anchoredto the hardware peripheral. Java application software can then use theobject using standard Java methods and procedures to control thehardware peripheral with all the benefits of hardware protection andabstraction that are provided in a normal Java virtual machine.Advantageously, faster development and integration can be realized usingsuch anchored arrays.

BRIEF DESCRIPTION OF THE DRAWINGS

Preferred embodiments of the invention will now be described withreference to the attached drawings in which:

FIG. 1 is a block diagram of a typical Java environment;

FIG. 2 is an example of a typical memory map including peripheral memorymapped registers;

FIGS. 3A to 3C depict typical Java objects and object managementstructures;

FIG. 3D is a typical memory map showing a portion of memory reserved forobjects;

FIG. 4 is a Java class provided by an embodiment of the invention forimplementing anchored Java objects;

FIG. 5 is another conventional Java object structure; and

FIG. 6 is a memory map provided by an embodiment of the inventionincluding peripheral memory mapped registers and Java object headerspace.

DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS

Communications from a processor to hardware peripherals are typicallydone through registers which are mapped to a predetermined address spaceof the processor. Embodiments of the invention provide systems andmethods for anchoring an object in Java, such as an array object, suchthat it overlaps with the register area of a hardware peripheral. By wayof example, FIG. 2 illustrates a typical memory map showing an entireaddress space running from a lowest address 47 0x0000 to a highestaddress 48 0xFFFF for a 64 kB memory address space. Usually, a firstportion 50 of the address space is reserved for ROM, a second portion 52of the address is reserved for RAM, and a third portion 54 of memoryaddress space is reserved for registers used by hardware peripherals.The mapping for a given peripheral, such as a serial port peripheral forexample, has been expanded, as generally indicated by 56. Typically,there are a number of addresses 58 (four in the illustrated example)which map to control registers which are used to control the peripheral.There are a number of addresses 60 (two in the illustrated example)mapped to data registers through which the data flow per se occurs.These data registers would not exist for peripherals to/from which dataflow is not to occur. Finally, there are typically a number of addresses62 (two in the illustrated example) mapped to registers for accessingstatus information. There is a separate portion of the peripheraladdress space 54 for each hardware peripheral.

FIGS. 3A to 3C illustrate some of the internal memory structures of aconventional Java virtual machine such as JVM 22 of FIG. 1. In FIG. 3A,generally indicated at 80 is a list of object handles 82. Each of thehandles 82 is an address which points to an object descriptor such asshown in FIG. 3B generally indicated by 84 which includes a header 85defining an object. In the case of an array, the header 85 contains atype field 86 (identifying the object to be an array and defining thesize of each element in the array and possibly other type information,and defining what type of Java garbage collection is to be performed onthe object), a base address field 88, and a length field 90 in units ofarray elements. The base address field 88 contains the start address ofarray elements in memory such as array elements 92 illustrated in FIG.3C. During normal operation, the length field 90 is compared withindexes into the array 92 to determine whether an exception must bethrown to signal an out of bounds access. In conventional systems theelements of the array structure 92 are abstracted from Java applicationswhich therefore can never refer to them directly. Rather, as shown inFIG. 3D a section 94 of memory (typically in RAM) between the addressesheap.start and heap.end is set aside for the dynamic allocation toobjects. The creation of an array at the application level wouldallocate a region of memory 96 selected from the available object memory94. With the allocation, the JVM would fill in the elements of structure84. Both the object handles 82 and the objects to which they refer arecreated and destroyed by the JVM 22.

According to an embodiment of the invention, a method is provided foranchoring a Java object, such as an array, to a specific area of memory,for example to a predetermined portion of memory address space forexample space mapped to a hardware peripheral. While the examplesprovided are Java specific, the invention can also be applied to otherapplication layer models which would otherwise restrict access to thespecific memory mapped locations. To achieve this, a new Java class isdefined, referred to herein as “class AnchoredArray” although of courseother names may be used.

Referring to FIG. 4, the new AnchoredArray class has a parameter list100 containing the elements base 104, length 106 which are used tocharacterize a particular hardware peripheral in the sense that the base104 is selected to be the base address of the registers in memoryassigned to the particular hardware peripheral, and length 106 specifieshow many elements there are in the hardware peripheral's memory mappedregisters. The class might optionally be designed to include a typeelement for specifying an array having a certain structure. In theabsence of a type, the class would need to assume a default type, forexample an array of integers. This class would, upon construction (whichwould normally occur during system initialization), use the parameterlist 100 to generate an array object descriptor 84 having an objectheader 85 (see FIG. 3B). (As an aside, it is noted that this differsfrom the construction of a normal array which is done by allocating amemory region 96 from the object memory 94 and then filling in theheader 85 after the fact.) More specifically, a default value specifyingan integer array, and the parameters base 104, and length 106 would becopied into corresponding type 86, base address 88, and length 90 fieldsin the header 85 of an array object descriptor 84. Once created, thearray object descriptor is indistinguishable from a normal Java arrayobject descriptor. The class has associated native code specificallywritten to achieve this function.

The array object descriptor 84 thus created would be stored in theobject memory 94, and a handle 82 to the array object descriptor 84 isadded to the list of object handles 80. At this point the memory spacedefined by the object descriptor 84 is anchored to the hardwareperipheral and the Java application software can access the array usingstandard Java methods and procedures to control the hardware peripheralwith all the benefits of hardware protection and abstraction that areprovided in a normal Java virtual machine.

It is noted that the list of handles 80 and the object descriptor 84 cantake many shapes and forms. The initialization can also take place whena class is loaded, or at boot time. Alternatively, a system class couldlet the system allocate a normal array and then replace the pointers. Aspecific example of pseudocode for implementing the AnchoredArray classis provided further below.

A second embodiment of the invention is provided for use when the memorystructure representing an object differs from that of FIGS. 3B and 3C.Referring to FIG. 5, the memory structure 120 for this embodiment has atype 122 and a length 124. Instead of a base address however, this isimmediately followed by a plurality of data elements 126 which make upthe body of the array. By taking away the base address, a level ofindirection has been removed. This second embodiment requires peripheralmemory mapped address space to conform to the defined object structure.

In order to map hardware peripheral address space directly to such anarray object, the memory map must include the additional fields lengthand type either adjacent to or as part of the normal hardware peripheralmemory mapped registers as depicted in FIG. 6 which is similar to thememory map of FIG. 2 except that additional registers 130,132 areprovided for type and length respectively. Thus a slight change to thememory map for the hardware peripherals is required.

Hence, the object descriptor of the structure 120 must forcefullyprepend the data elements, whereas the use of structure 84 provided anadditional level of indirection. The absence of this additional level ofindirection makes accesses to elements of the array 120 faster, but alsoforces the hardware peripheral address space shown in FIG. 6 to containadditional registers for the type and length fields 130,132.

These two additional registers 130,132 may be hardcoded in hardware orinitialized using a dedicated base class. At initialization time, thededicated base class ensures an object handle 72 refers directly to thebase of the peripheral registers, at which point application softwarecan use the array using standard Java methods and procedures to controlthe hardware devices, with all the benefits of hardware protection andabstraction that are provided in a Java virtual machine.

In another embodiment, rather than having two additional registers130,132 for each peripheral, only two additional registers are providedfor the peripheral address space collectively. Java threads reading orwriting to the single composite object thus created would need to knowwhich subgroup of registers to use. While this protects non-peripheralmemory space from incorrect access, it does not prevent a thread workingwith one peripheral from erroneously accessing registers belonging toanother peripheral.

In the Java environment, a consideration which might need to be dealtwith during the construction of objects, and for our purposes theconstruction of objects which map to peripheral address spaces, isgarbage collection. Garbage collection is the process through which theJVM performs housekeeping on the object memory. The details of Javagarbage collection are well known and will not be repeated here. Inorder to prevent objects being garbage collected during their creation,the objects should be created as static objects using staticinitialization blocks. In the event dynamic objects are used, thengarbage collection issues would need to be addressed. The dynamicobjects might be created using the static root mechanism for example,which is also well known.

By way of example, the following is Pseudocode for an implementation ofthe AnchoredArray class for the case where the object structure of FIGS.3B and 3C is employed and where the default is that the array is aninteger array. The Java class is compiled with the rest of the system.Device driver classes can make use of it to access hardware.

Class AnchoredArray

{

//element is an array handle which will be mapped directly onto theperipheral.

public int element[ ];

//The constructor takes two parameters, a base address and a length

//The function of the constructor is to initialize element [ ] usingthose parameters.

public AnchoredArrayint(baseAddress, int length)

{

//The action of mapping an array onto a specific area of memory is doneat the native

//level. Since a Java constructor cannot be native, the constructorcalls a native

//function lockDownElements( ) to initialize the array.

element=lockDownElementsbase(Address,length);

//This native function instantiates an array on a fixed memory area. Itis made static

//because it does not use the class instance.

Static private native int[ ] lockDownElements(int baseAddress, intlength);

}

void AnchoredArray_lockDownelements( )

{

//first, we get the two parameters off the stack. All the parameters inJava are

//pushed onto the stack by the caller, and popped from the stack by thenative

//method. Similarly, the native functions push the result onto the stackfor the caller

//to retrieve.

int base=popStack( );

int length=popStack( );

//in the case where there is an indirection, we would normally allocatespace for array

//elements but in this case we don't because the base indicates wherethe elements

//are. We just create the object that points to the elements.

int *handle=malloc(SIZE_OF_HEADER);

(instance*) handle->type=DEFAULT_TYPE

(arrayStruct*) handle->arrayBase=base;

(arrayStruct*) handle->length=length;

//That's it. We are now passing the handle to Java.

push handle;

}

Pseudocode follows for an example implementation of the AnchoredArrayclass and associated lockDownElements native function for the case whereobjects have the structure of FIG. 5, i.e. the length, base informationis to be stored in the memory mapped registers adjacent the remainingregisters.Class AnchoredArray{public int element[ ];public AnchoredArray(int baseAddress)

{

element=lockDownElements(baseAddress);

Static private native int[ ] lockDownElements(int baseAddress);

}

Void AnchoredArray_lockdownElements( )

{

//first, we get the parameter off the stack. All the parameters in Javaare

//pushed onto the stack by the caller, and popped from the stack by thenative

//method. Similarly, the native functions pushes the result onto thestack for the caller

//to retrieve.

int base=popStack( );

//the beginning of the peripheral. In that case, the length field is notused.

pushStack(base);

//that's it. We are now passing the handle to the hardcoded object toJava.

}

Referring back to the memory map example of FIG. 2 and in particular tothe memory map details for the serial port, the base address for theserial port's address space is SPORT_BASE=0xFF00, the length isSPORT_LENGTH=8. These values would be used in the parameter list 100when constructing the AnchoredArray class for the serial port.

The JVM would be designed to include the above discussed AnchoredArrayclass as part of its system classes, and for the serial port peripheral,there would be a constructor:

AnchoredArray serial_port=new AnchoredArray(SPORT_BASE, SPORT_LENGTH)

where SPORT_BASE is a constant in this case equal to 0xFF00, andSPORT_LENGTH is a constant in this case equal to 8 which would result ina handle 82 pointing to the serial_port anchored array (i.e. pointing to0xFF00) being added to the list of handles 80 at initialization.Subsequently, indexes into the array may be done for example throughexpressions such as serial_port.element[C1], where C1 is a constantwhich indexes into the array to the register occupied by the first ofthe peripheral's constants 64, which would in turn provide access tothat register. The Java range checking functionality would permit accessthrough this particular handle only to the specific range of addressesdefined. An attempt to access an index which would point to memoryaddresses outside this space would result in an exception.

Optionally, the AnchoredArray class can be declared so that applicationclasses cannot call it if they are not part of the same Java package.

Optionally, the AnchoredArray class can be made public to allow driverclasses to be put in any package and use the AnchoredArray classnonetheless.

Optionally, the AnchoredArray constructor can be protected so it canonly be invoked by derived classes.

Numerous modifications and variations of the present invention arepossible in light of the above teachings. It is therefore to beunderstood that within the scope of the appended claims, the inventionmay be practised otherwise than as specifically described herein.

1. A computer implemented method of providing Java application layeraccess a fixed memory address space comprising: constructing a Javaobject having elements which occupy said fixed memory address space;whereby a Java application is provided access to the hardwareperipheral's memory mapped registers directly through said Java object;wherein constructing a Java object comprises: creating an objectdescriptor; creating an object handle for the Java object which pointsto the object descriptor defining a new Java class having base address,length, and type parameters, which is used in constructing said Javaobject; and wherein new class is substantially defined in pseudocode asfollows: Class AnchoredArray { public int element[ ]; publicAnchoredArray(int baseaddress, int length) {element=lockDownElements(baseAdress,length); Static private native int[] lockDownElements(int baseAddress, int length); } voidAnchoredArray_lockDownelements( ) { int base=popstack( ); intlength=popstack( ); int *handle=malloc(SIZE_OF_HEADER); (instance*)handle->type=DEFAULT_TYPE; (arrayStruct*) handle->arrayBase=base;(arrayStruct*) handle->length=length; push handle; }.
 2. A computerimplemented method of providing lava application layer access a fixedmemory address space comprising: constructing a Java object havingelements which occupy said fixed memory address space; whereby a Javaapplication is provided access to the hardware peripheral's memorymapped registers directly through said Java object; identifying thefixed memory address space to be a hardware peripheral's memory mappedregisters; wherein constructing a lava object comprises: defining amemory map having a predetermined address space for the hardwareperipheral, and allocating at least one additional address spacecontiguous with the predetermined address space; storing object headerinformation for the Java object directly in the additional addressspace; creating an object handle for the lava object which points to theobject header; defining a new Java class having a base address parameterwhich is used in constructing said Java object; wherein the new Javaclass is substantially defined in pseudocode as follows: ClassAnchoredArray { public int element[ ]; public AnchoredArray(intbaseAddress) { element=lockDownElements(type,baseAddress); Staticprivate native int[ ] lockDownElements(int baseAddress); } VoidAnchoredArray-lockdownElements( ) { int base=popstack( );pushStack(base); }.
 3. A computer implemented method of providingapplication layer access to a fixed memory address space for anapplication in a language designed to prevent accessing particularmemory locations directly, the method comprising: constructing an objectin the context of the language having elements which occupy said fixedmemory address space; whereby the application is provided access to thefixed memory address space directly through said object; defining a newclass having a class name <class name> as follows: <class name> (base,length); where <class name> is the name assigned to the new class, baseis a parameter which specifies a type of object, base is a parameterwhich specifies a beginning address, and length is a parameterspecifying a number of elements in the object, which when constructed,generates an object descriptor specifying base, length, and a generatesa handle which points to the object descriptor; wherein the new class issubstantially defined in pseudocode as follows: Class AnchoredArray {public int element[ ]; public AnchoredArray(int baseaddress, int length){ element=lockDownElements(baseAdress,length); Static private nativeint[ ] lockDownElements(int baseAddress, int length); } voidAnchoredArray_lockDownelements( ) { int base=popstack( ); intlength=popstack( ); int *handle=malloc(SIZE_OF_HEADER); (instance*)handle->type=DEFAULT_TYPE; (arrayStruct*) handle->arrayBase=base;(arrayStruct*) handle->length=length; push handle; }.
 4. A computerimplemented method of providing application layer access to a fixedmemory address space for an application in a language designed toprevent accessing particular memory locations directly, the methodcomprising: constructing an object in the context of the language havingelements which occupy said fixed memory address space; whereby theapplication is provided access to the fixed memory address spacedirectly through said object; identifying the fixed memory address spaceto be a hardware peripheral's memory mapped registers; whereinconstructing an object comprises: defining a memory map having apredetermined address space for the hardware peripheral, and allocatingat least one additional address space contiguous with the predeterminedaddress space; storing object header information for the Java objectdirectly in the additional address space; creating an object handle forthe object which points to the object header; defining a new classhaving a base address parameter which is used in constructing saidobject; wherein the new class is substantially defined in pseudocode asfollows: Class AnchoredArray { public int element[ ]; publicAnchoredArray(int baseAddress) {element=lockDownElements(type,baseAddress); Static private native int[ ]lockDownElements(int baseAddress); } VoidAnchoredArray_lockdownElements( ) { int base=popstack( );pushStack(base); }.